package Candidate;

import ibatis.IbatisHelper;

import java.util.*;
import java.sql.*;

import com.ibatis.sqlmap.client.SqlMapClient;

public class CandidateManager {
	private SqlMapClient ibatis;

	public CandidateManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}
	public List selectAllCandidate(CandidateForm cForm) {
		List result = null;

		try {
			Map map = new HashMap();

			map.put("param_regno",cForm.getParam_regno());
			map.put("param_fullname", cForm.getParam_fullname());
			map.put("param_techtestplace", cForm.getParam_techtestplace());
			map.put("param_techtestdate", cForm.getParam_techtestdate());
			map.put("param_email", cForm.getParam_email());
			map.put("param_jobcode",cForm.getParam_jobcode());
			result = ibatis.queryForList("candidate.selectAllCandidate", map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public List getAcceptedCandidate(String status, Integer batchId) {

		List result = null;

		try {
			Map map = new HashMap();
			map.put("status", status);
			map.put("batchId", batchId);

			result = ibatis.queryForList("candidate.getAcceptedCandidate", map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public List selectAllJob() {
		List result = null;

		try {
			result = ibatis.queryForList("candidate.selectAllJob", "");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public List selectAllSchoolData() {
		List result = null;

		try {
			result = ibatis.queryForList("candidate.selectAllSchoolData", "");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public List selectAllCityData() {
		List result = null;

		try {
			result = ibatis.queryForList("candidate.selectAllCityData", "");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public List selectAllBatchData() {
		List result = null;

		try {
			result = ibatis.queryForList("candidate.selectAllBatchData", "");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public List selectBatch() {
		List result = null;

		try {
			result = ibatis.queryForList("candidate.selectBatch", "");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public boolean insertCandidate(CandidateModel cModel) {

		boolean flag;
		try {
			ibatis.startTransaction();
			this.ibatis.insert("candidate.insertCandidate", cModel);
			flag = true;
			ibatis.commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return flag;
	}

	public boolean insertAccCand(CandidateModel cModel) {

		boolean flag;
		try {
			ibatis.startTransaction();
			this.ibatis.insert("candidate.insertAccCand", cModel);
			flag = true;
			ibatis.commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return flag;
	}
	
	public boolean insertSchool(CandidateModel cModel) {
		boolean flag = false;
		try {
			ibatis.startTransaction();

			this.ibatis.insert("candidate.insertSchool", cModel);
			ibatis.commitTransaction();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
				flag = false;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return flag;
	}

	public boolean insertCity(CandidateModel cModel) {
		boolean flag;
		try {
			ibatis.startTransaction();

			this.ibatis.insert("candidate.insertCity", cModel);
			ibatis.commitTransaction();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return flag;
	}

	public boolean insertMsUser(CandidateModel cModel) {

		try {
			ibatis.startTransaction();

			this.ibatis.insert("candidate.insertMsUser", cModel);
			ibatis.commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return true;
	}

	public String getRegNo() {
		String result=null;
		try {
			result = (String) ibatis.queryForObject("candidate.getRegNo", "");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public String getCity(Integer cityId) {
		String result = null;
		try {
			result = ibatis.queryForObject("candidate.getCity", cityId).toString();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public String getSchoolCity(Integer schoolId) {
		String result = null;
		try {
			result = ibatis.queryForObject("candidate.getSchoolCity", schoolId).toString();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public Integer getSchoolId(String schoolName) {
		Integer result = null;
		try {
			result = Integer.parseInt(ibatis.queryForObject("candidate.getSchoolId", schoolName).toString());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public String getRegCheck(String emailCandidate,String idCardNum,String userIdCardNum) {
		String result = null;
		try {
			Map map = new HashMap();
			
			map.put("emailCandidate", emailCandidate);
			map.put("idCardNum", idCardNum);
			map.put("userIdCardNum", userIdCardNum);
			
			result = (String) ibatis.queryForObject("candidate.registrationCheck",map);
		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public Integer getCityId(String city) {
		Integer result = null;
		try {
			result = (Integer) ibatis.queryForObject("candidate.getCityId", city);

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public Integer isUserId(String userId) {
		Integer result = null;
		try {
			result = Integer.parseInt(ibatis.queryForObject("candidate.isUserId", userId).toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public Integer getBatchId(String batch) {
		Integer result = null;
		try {
			result = Integer.parseInt(ibatis.queryForObject("candidate.getBatchId", batch).toString());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public Integer checkPasswordUser(String userId,String oldPassword) {
		Integer result = null;
		try {
			HashMap map = new HashMap();
			map.put("userId", userId);
			map.put("password", oldPassword);

			result = Integer.parseInt(ibatis.queryForObject("candidate.getPasswordUser", map).toString());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean updateAccount(CandidateModel cModel) {
		boolean flag;
		try {

			ibatis.startTransaction();
			this.ibatis.update("candidate.updateAccount", cModel);

			ibatis.commitTransaction();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return flag;
	}
	
	public boolean updateCandidate(CandidateModel cModel) {
	
		boolean flag;
		try {
			ibatis.startTransaction();
			this.ibatis.update("candidate.update", cModel);

			ibatis.commitTransaction();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return flag;
	}

	public boolean resetCandidate(String userId) {
		boolean flag;
		try {

			ibatis.startTransaction();

			this.ibatis.update("candidate.resetCandidate", userId);
			ibatis.commitTransaction();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}

	public boolean resetTechTest(String userId) {
		boolean flag;
		try {

			ibatis.startTransaction();

			this.ibatis.delete("candidate.resetTechTest",userId);
			ibatis.commitTransaction();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
	public boolean updateLastStatus(String regisNo) {
		boolean flag;
		try {
			String status = "No Status";
			String lastStatus = "Technical Test";
			ibatis.startTransaction();

			HashMap map = new HashMap();
			map.put("status", status);
			map.put("regisNo", regisNo);
			map.put("lastStatus", lastStatus);

			this.ibatis.update("candidate.updateLastStatus", map);

			ibatis.commitTransaction();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}

	public CandidateModel selectCandidate(String userId) {
		CandidateModel bean = null;

		try {
			bean = (CandidateModel) ibatis.queryForObject(
					"candidate.getCandidate", userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bean;
	}
	
	public boolean resetRegNoSeq() {
		boolean flag;
		try {
			ibatis.startTransaction();
			this.ibatis.update("candidate.resetRegNoSeq", "");
			ibatis.commitTransaction();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}

	public CandidateModel selectCandidateWithOther(String userId){
		CandidateModel bean = null;
		
		try {
			bean=(CandidateModel) ibatis.queryForObject("candidate.getCandidateWithOthers", userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();                                                              
		}

		return bean;
	}
	
	public List getBatch() {
		List result = null;

		try {
			result = ibatis.queryForList("candidate.getAllBatch", "");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List getTraineeList(String batch){
		List result = null;
		try {
			HashMap map = new HashMap();
			map.put("batch", batch);
			result = ibatis.queryForList("candidate.getTraineeList", map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public List getTraineeSearch(String keyword,String category){
		List result = null;
		try {
			System.out.println(keyword);
			System.out.println(category);
			HashMap map = new HashMap();
			map.put("keyword", keyword);
			map.put("category", category);
			result = ibatis.queryForList("candidate.getTraineeSearch", map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	
	public CandidateModel viewTrainee(String traineeId) {
		List result = null;
		CandidateModel bean = null;
		try {
			bean = (CandidateModel) ibatis.queryForObject("candidate.getViewTrainee",
					traineeId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bean;
	}
}